//
//  MyDataBaseHandle.m
//  真·豆瓣半完全体
//
//  Created by dllo on 16/1/8.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "MyDataBaseHandle.h"
#import <sqlite3.h>
#import "collectModel.h"
@implementation MyDataBaseHandle

+ (MyDataBaseHandle*) sharedMyDataBaseHandle{
    static MyDataBaseHandle *handle = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        handle = [[MyDataBaseHandle alloc] init];
    });
    return handle;
}

- (void) openDataBase{
    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *dbPath = [document stringByAppendingPathComponent:@"Movie.sqlite"];
    NSLog(@"Movie沙盒：%@", dbPath);
    int result = sqlite3_open(dbPath.UTF8String, &dbPoint);
    if (result == SQLITE_OK) {
        NSLog(@"OpenSuccess");
    }
    else{
        NSLog(@"OpenDefeat");
    }
}
- (void) createTable{
    NSString *sqlStr =[ NSString stringWithFormat:@"create table if not exists Movie(number integer primary key autoincrement, title text)"];
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"CreateSuccess");
    }
    else{
        NSLog(@"CreatDefeat");
    }
}
- (void) insertModel:(collectModel *)model{
    NSString *sqlStr = [NSString stringWithFormat:@"insert into Movie(title) values('%@')", model.title];
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"InsertSuccess");
    }
    else{
        NSLog(@"InsertDefeat");
    }
}
- (NSMutableArray *) selectModle{
    NSString *sqlStr = [NSString stringWithFormat:@"select * from Movie"];
    NSMutableArray *arr =[ NSMutableArray array];
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare_v2(dbPoint, sqlStr.UTF8String, -1, &stmt, NULL);
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *title = sqlite3_column_text(stmt, 1);
            collectModel *model = [collectModel collectModel];
            model.title = [NSString stringWithUTF8String:(const char*)title];
            [arr addObject:model];
        }
    }
    else{
        NSLog(@"SelectDefeat");
    }
    return arr ;
}

- (void) deleteModel{
    NSString *sqlStr = [NSString stringWithFormat:@"delete from Movie"];
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"DeleteSuccess");
    }
    else{
        NSLog(@"DeleteDefeat");
    }
}

- (BOOL) selectModle:(collectModel *) model{
    NSString *sqlStr = [NSString stringWithFormat:@"select * from Movie where title = ('%@')", model.title];
    //    NSMutableArray *arr =[ NSMutableArray array];
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare_v2(dbPoint, sqlStr.UTF8String, -1, &stmt, NULL);
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *title = sqlite3_column_text(stmt, 1);
            collectModel *model = [collectModel collectModel];
            model.title = [NSString stringWithUTF8String:(const char*)title];
            //            [arr addObject:model];
            return YES;
        }
    }
    else{
        return NO;
    }
    return nil;
}

@end
